package com.aosiding.crudmp.controller;


import com.aosiding.crudmp.entity.User;
import com.aosiding.crudmp.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author AOSIDING
 * @since 2021-05-21
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/getAllUser")
    public List<User> getAll(){
        List<User> list = userService.list(null);
        return list;
    }

    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable Long id){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        return userService.getOne(wrapper);
    }

    @DeleteMapping("/deleteById/{id}")
    public boolean deleteById(@PathVariable Long id){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        return userService.remove(wrapper);
    }

    @PostMapping("/createUser")
    public boolean createUser(@RequestBody User user){
        boolean flag = userService.save(user);
        return flag;
    }

    @PostMapping("/UpdateUser")
    public boolean updateUser(@RequestBody User user){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id",user.getId());
        return userService.update(user,wrapper);
    }

}

